/*
 * Copyright (c) 2016 UBG - 版权所有
 * 
 * This software is the confidential and proprietary information of
 * UBG. You shall not disclose such confidential information 
 * and shall use it only in accordance with the terms of the license 
 * agreement you entered into with www.7qjf.com.
 */
package bigdata.propertiesobj;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
import java.util.WeakHashMap;

import common.utils.LogUtil;
import org.apache.commons.logging.Log;


/**
 *
 * 描述: properties文件读取工具类
 *
 * @created 2016年11月24日 上午10:11:56
 * @since   v1.0.0
 */
public class PropertiesUtils {

	private static WeakHashMap<String, Properties> cache = new WeakHashMap<String,Properties>();
	
	private static String ROOT_PATH = PropertiesUtils.class.getResource("/").getPath();
	
	private static Log propertieLog = LogUtil.getPropertiesLog();

	/**工具类，不允许实例化**/
	private PropertiesUtils() {}
	
	/**
	 * 
	 *	
	 * 描述:初始化properties
	 *
	 * @author  001298
	 * @created 2016年11月25日 上午11:42:12
	 * @since   v1.0.0 
	 * @param source
	 * @return
	 * @return  Properties
	 */
	public static Properties init(String source) {
		Properties prop = new Properties();
		try {
			String path = ROOT_PATH + source;
			if(cache.containsKey(path)) {
				prop = cache.get(path);
				if(prop == null) {
					prop = load(path);
					cache.put(path, prop);
				}
				propertieLog.info("get " + path + "from cache");
				return prop;
			}
			prop = load(path);
			cache.put(path, prop);
			return prop;
		} catch (Exception e) {
			propertieLog.error("can't find properties file ... , source = " + source);
			e.printStackTrace();
		}
		return prop;
	}
	
	private static Properties load(String path) {
		Properties prop = new Properties();
		try {
			InputStream in = new FileInputStream(new File(path));
			System.out.println("loading ..." + path);
	        prop.load(in);
		} catch (Exception e) {
			propertieLog.error("can't find properties file ... , source = " + path);
			e.printStackTrace();
		}
		return prop;
	}
	
	
	
	
	public static Properties refresh(String source) {
		Properties prop = new Properties();
		String path = ROOT_PATH + getPath(source);
		System.out.println("source = " + source);
		prop = load(path);
		cache.put(path, prop);
		Iterator<Object> it = cache.get(path).keySet().iterator();
		while(it.hasNext()) {
			String key = String.valueOf(it.next());
			System.out.println("key:" + key + " value: " + prop.getProperty(key));
		}
		return prop;
	}
	
	private static String getPath(String source) {
		if(PropertiesConstant.EMAIL_TYPE.equalsIgnoreCase(source)) {
			return PropertiesConstant.EMAIL;
		} else if(PropertiesConstant.CONFIG_TYPE.equalsIgnoreCase(source)) {
			return PropertiesConstant.CONFIG;
		} else if(PropertiesConstant.REDIS_TYPE.equalsIgnoreCase(source)) {
			return PropertiesConstant.REDIS;
		} else if(PropertiesConstant.PHONECODE_TYPE.equalsIgnoreCase(source)) {
			return PropertiesConstant.PHONECODE;
		} else if(PropertiesConstant.ACTIVEMQ_TYPE.equalsIgnoreCase(source)) {
			return PropertiesConstant.ACTIVEMQ;
		}
		return source;
	}
	
	
}
